/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.trainee.aplikasipenjualan.service.impl;

import com.trainee.aplikasipenjualan.entity.Barang;
import com.trainee.aplikasipenjualan.entity.JualHeader;
import com.trainee.aplikasipenjualan.entity.PenjualanDetail;
import com.trainee.aplikasipenjualan.service.AplikasipenjualanService;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 *
 * @author Jaka
 * @author Abdul Rozak
 */
@Service("aplikasipenjualanService")
@Transactional(readOnly=true)
public class AplikasipenjualanServiceImpl implements AplikasipenjualanService {

    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    @Transactional(readOnly=false)
    public void saveBarang(Barang barang) {
        sessionFactory.getCurrentSession().saveOrUpdate(barang);
    }

    @Override
   @Transactional(readOnly=false)
    public void deleteBarang(Barang barang) {
        sessionFactory.getCurrentSession().delete(barang);
    }

    @SuppressWarnings("unChecked")
    @Override
    public List<Barang> findAllBarang() {
         return sessionFactory.getCurrentSession().createQuery("from Barang").list();
    }

    @Override
    public Barang findAllBarangById(String id) {
        
        Barang b = (Barang) sessionFactory.getCurrentSession()
                .createQuery("from Barang b where b.kodeBrg = :kd")
                .setParameter("kd", id)
                .uniqueResult();
        Hibernate.initialize(b.getDetail());
        return b;
    }
    
    @Override
    public void deletePenjualan() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<JualHeader> findAllPenjualan() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public JualHeader findAllPenjualanById(String id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    @Transactional(readOnly=false)
    public void savePenjualan(PenjualanDetail pd) {
        sessionFactory.getCurrentSession().saveOrUpdate(pd);
    }
    
}
